//
//  ShowUserSearchViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/23.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ShowUserSearchViewController.h"
#import "ShowUserSearchTableViewCell.h"
#import "ShowUserSearchModel.h"
#import "ZWYNetTool.h"
#import "ShowUserSearchRes.h"

#define kShowUserSearchTableViewCellName @"ShowUserSearchTableViewCellNameReuse"
@interface ShowUserSearchViewController ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) ShowUserSearchModel *model;
@property (nonatomic, assign) NSInteger bodyNum;

@end

@implementation ShowUserSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"查找用户";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = back;
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 10;
    _textField.delegate = self;
    _textField.returnKeyType = UIReturnKeySearch;
    _textField.enablesReturnKeyAutomatically = YES;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ShowUserSearchTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kShowUserSearchTableViewCellName];
    _bodyNum = 1;
    NSString *bodyString = [NSString stringWithFormat:@"page=%ld", _bodyNum];
    NSString *lengthString = [NSString stringWithFormat:@"%ld", [bodyString length]];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : lengthString};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=user1&a=recommendUser" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [ShowUserSearchModel modelWithDic:result];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    
}
- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShowUserSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kShowUserSearchTableViewCellName];
    cell.res = _model.res[indexPath.row];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"推荐用户:";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self resignTextResponder];
    if (textField.returnKeyType == UIReturnKeySearch) {
        if ([_textField.text length] > 0) {
            NSInteger num = _bodyNum;
            _bodyNum = 1;
            NSString *textFieldString = [_textField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSString *bodyString = [NSString stringWithFormat:@"nickname=%@&page=%ld", textFieldString, _bodyNum];
            NSString *bodyLength = [NSString stringWithFormat:@"%ld", [bodyString length]];
            NSDictionary *dic = @{@"version" : @"2.50",
                                  @"Content-Type" : @"application/x-www-form-urlencoded",
                                  @"Content-Length" : bodyLength};
            [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=user1&a=search" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
                _model = [ShowUserSearchModel modelWithDic:result];
                if (_model.code == 200) {
                    [_tableView reloadData];
                } else {
                    _bodyNum = num;
                }
            } andFailureBlock:^(NSError *error) {
                NSLog(@"请求失败");
            }];
            
            
        }
    }
    return YES;
}
- (void)resignTextResponder {
    if ([_textField isFirstResponder])
    {
        [_textField resignFirstResponder];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
